// pages/yjyd/yjyd.js
const app = getApp();
const sendMessage = require('../home/home.js')
const util = require('../../utils/util')
const audioCtx = wx.createInnerAudioContext();    // 创建音频上下文对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second:'300',
    times:0,
    trainType:'',
    speed:'',
    url:'',
    bgm:false,
    timer:'',
    inter:'',
    equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.startInterval();
    var that = this;
    var speed = options.speed;
    var trainType = options.trainType;
    that.setData({
      trainType:trainType,
      speed:speed,
      bgm:true,
    })
    this.startCountdown();
    //this.bgm();
  },
  startCountdown() {
    var that = this;
    var second = that.data.second;
    var equipmentNumber = app.nowEquipmentId;
    if (second == 0) {
      clearInterval(that.data.timer);
      clearInterval(that.data.inter);
      that.setData({
        bgm:false
      })
      //this.bgm();
      var score = 5;
      wx.navigateTo({
        url: '../fraction/fraction?fraction='+score,
      })
    }
    that.data.timer = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.startCountdown();
    } , 1000)
    var times = that.data.times;
    times++;
    that.setData({
      times:times,
      equipmentNumber:equipmentNumber
    })
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"眼肌运动",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    this.setData({
      bgm:false
    })
    // this.bgm();
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"眼肌运动",
      openId:openId
    })
    util.saveRecord(this);
  },
  // bgm:function(e){
  //   console.log(this.data.bgm);
  //   if (this.data.bgm == false){
  //     console.log("停止");
  //     this.setData({
  //       bgm: false,
  //       })
  //       audioCtx.pause();
  //       audioCtx.stop();
  //   } else {
  //     console.log("播放");
  //     this.setData({
  //       bgm: true,
  //       })
  //       audioCtx.autoplay = true;  
  //       audioCtx.src = 'https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/%E5%8F%A4%E5%85%B8%E7%8B%82%E6%AC%A2%E8%8A%82_YouTube%28%E7%9C%BC%E8%82%8C%E8%BF%90%E5%8A%A8%29.mp3';  // 设置音频地址   
  //   }
  //  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})